import axios, {AxiosResponse} from "axios";
import { IPageInfoRsp} from "@/lib/recover";
const debug = false;
interface Rsp<T>{
    items:T[],
    page_info:IPageInfoRsp,
    total_count:number,
}
async function *getData<T>(path:string,page:number,args:string="") {
    while(true) {
        const {data} = await axios.get<Rsp<T>,AxiosResponse<Rsp<T>>>(path+"?page="+page+"&size=10"+args)
        page++;
        yield data;
    }
}

/**
 * 一次性拉取全部数据
 * @param path
 * @param page
 * @param args 以'&arge1=12'的形式传入
 */
export async function getAllData<T>(path:string,page:number,args:string=""): Promise<T[]> {
    const obj:T[] = [];
    for await(const data of getData<T>(path,0,args)) {
        if(debug) {
            console.log(data.items);
        }
        if(data.items.length != 0) {
            obj.push(...data.items);
        }
        if(!data.page_info.has_next_page){
            break;
        }
    }
    return obj

}